package com.example.yyy

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import android.view.WindowManager
import androidx.core.app.NotificationCompat
import org.jetbrains.anko.intentFor

class SendFloatNotify(context: Context,title:String,message:String) {

        init {
            //声明一个点击通知消息时触发的动作意图
            val clickIntent = context.intentFor<MainActivity>()
            val piClick = PendingIntent.getActivity(
                context,
                R.string.app_name,
                clickIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )
            //构建消息参数
            val builder: NotificationCompat.Builder = NotificationCompat.Builder(context, "7")
            builder.setContentIntent(piClick)
                .setAutoCancel(true)
                .setTicker("浮动消息")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.tp))
                .setContentTitle(title)
                .setContentText(message)
                .setFullScreenIntent(piClick,true)
            //获取通知管理器
            val manager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //andriod 8 之后需要一个渠道
                val channel = NotificationChannel("4", "channel4", NotificationManager.IMPORTANCE_DEFAULT)
                channel.enableLights(true)
                channel.lightColor = Color.RED
                channel.setShowBadge(true)
                manager.createNotificationChannel(channel)
                builder.setChannelId("4")
            }
            val n = builder.build()
            manager.notify(4, n)
    }

}